/*
 * Script to manage logging in to the admin interface
 * 
 * Author: Chris Casola
 */

window.onload = function() {
	window.comm = new Communicator();
	//window.comm.onsend = function(request) { alert("about to send: " + JSON.stringify(request))};
	window.comm.onmessage = handleResponse;
	window.comm.onerror = function () { alert("received error message") };
	window.comm.start();
	document.getElementById('login').addEventListener("click", sendLoginRequest, false);
}

function handleResponse(response) {	
	if (response[0].success == "true") {
		sessionStorage['adminKey'] = response[0].adminResponse.key;
		window.location= "/manage/console.html";
	}
}

function sendLoginRequest() {
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	
	var request = {
		__XMLclass : "adminRequest",
		__XMLchildren : [{
			__XMLclass : "user",
			name : username,
			password : password
		}] 
	}
	window.comm.send("/do", new xmlRequest(request));
}

/*
 * Wrap the given request in a generic request
 * and set the request id.
 */
function xmlRequest(innerRequest) {
	this.__XMLclass = "request";
	this.version = "1.0";
	this.id = uuid.v4();
	this.__XMLchildren = [
		innerRequest
	];
}
